// DlgLogon.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgLogon.h"

// CDlgLogon dialog

IMPLEMENT_DYNAMIC(CDlgLogon, CDialog)

CDlgLogon::CDlgLogon(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgLogon::IDD, pParent)
	, m_szUID(_T(""))
	, m_szPWD(_T(""))
	, m_bSavePass(FALSE)
	, m_szVersion(_T(""))
{
}

CDlgLogon::~CDlgLogon()
{
}

void CDlgLogon::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_UID, m_szUID);
	DDX_Text(pDX, IDC_EDIT_PWD, m_szPWD);
	DDX_Check(pDX, IDC_CHECK_SAVE_PWD, m_bSavePass);
	DDX_Text(pDX, IDC_STATIC_VERSION, m_szVersion);
	DDX_Control(pDX, IDC_EDIT_PWD, m_ctrlPassword);
}


BEGIN_MESSAGE_MAP(CDlgLogon, CDialog)
END_MESSAGE_MAP()


// CDlgLogon message handlers

BOOL CDlgLogon::OnInitDialog()
{
	CDialog::OnInitDialog();

	// set to always on top
	//SetWindowPos( &wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE );

	// set version
	m_szVersion.LoadString( IDS_CELSUS_VERSION );

	// check for LDAP logon and get settings
	if( !appConfig.m_LDAP.bUseLDAPLogons )
	{
		//set type indicator
		GetDlgItem( IDC_STATIC_TYPE )->SetWindowText( _T("") );
	}
	else
	{
		//set type indicator
		GetDlgItem( IDC_STATIC_USER )->SetWindowText( _T("LAN ID") );
		GetDlgItem( IDC_STATIC_TYPE )->SetWindowText( _T("(LDAP)") );
		//// no saving password
		//GetDlgItem( IDC_CHECK_SAVE_PWD )->EnableWindow( FALSE );		
		//m_bSavePass = FALSE;
	}

	// get user name either way
	m_szUID = AfxGetApp()->GetProfileString( _T("Settings"), _T("User") );
	// get save password
	m_bSavePass = AfxGetApp()->GetProfileInt( _T("Settings"), _T("SavePassword"), 0 );

	// get the password
	if( m_bSavePass )
	{
		CString szPassword;
		szPassword = AfxGetApp()->GetProfileString( _T("Settings"), _T("Password") );
		// decrypt password
		if( !szPassword.IsEmpty() )
			appConfig.EnCrypt( (LPTSTR)(LPCTSTR)szPassword );
		m_szPWD = szPassword;
	}

	// update the data
	UpdateData( FALSE );

	// set focus to the password
	if( !m_szUID.IsEmpty() )
	{
		m_ctrlPassword.SetFocus();
		m_ctrlPassword.SetSel( 0, -1 );
		return FALSE;
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgLogon::OnOK()
{
	// update the data
	UpdateData();

	// trim user
	m_szUID.Trim();
	// check password
	if( m_szPWD.IsEmpty() )
	{
		AfxMessageBox( IDS_ERROR_PASSWORD, MB_OK | MB_ICONSTOP );
		return;
	}

	// save settings
	AfxGetApp()->WriteProfileInt( _T("Settings"), _T("SavePassword"), m_bSavePass );

	// save password
	if( m_bSavePass )
	{
		CString szPassword = m_szPWD;
		appConfig.EnCrypt( (LPTSTR)(LPCTSTR)szPassword );
		AfxGetApp()->WriteProfileString( _T("Settings"), _T("Password"), szPassword );
	}
	else
	{
		AfxGetApp()->WriteProfileString( _T("Settings"), _T("Password"), _T("") );
	}

	// check and update databse
	if( appConfig.m_Database.bUpdateOnLogin )
	{		
		SetDlgItemText( IDC_STATIC_VERSION, _T("Checking database version...") );
		appConfig.m_SQL.UpdateDatabase();
	}

	// map drive	
	if( !appConfig.m_UserInfo.szUserName.IsEmpty() )
	{
		SetDlgItemText( IDC_STATIC_VERSION, _T("Mapping network drive...") );
		appConfig.MapDrive( appConfig.m_General.szRootFolder, TRUE, appConfig.m_UserInfo.szDomain, appConfig.m_UserInfo.szUserName, appConfig.m_UserInfo.szPassword );
	}

	// save user name either way
	AfxGetApp()->WriteProfileString( _T("Settings"), _T("User"), m_szUID );

	CDialog::OnOK();
}

void CDlgLogon::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnCancel();
}
